package controllers

import (
	"github.com/kataras/iris"
	"irisfarm/app/repositories"
	"irisfarm/app/validates"
)

type UserController struct {
	BaseController
}

/**
 * @api {get} /api/user/login  登录
 *
 * @apiGroup LiveGroup
 *
 * @apiParam {string}  username  用户名
 * @apiParam {string}  password  密码
 *
 * @apiVersion 1.0.0
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *{
 *    "status_code": 200,
 *    "message": "成功",
 *    "data": {
 *			"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJcdTAwMDEifQ.UvJ6-UIUrXz73iaOsWczH5Yg1ipvWnpOxgHGxNPb32g",
 *			"uid": "1",
 *      	"username": "ken"
 *    }
 *}
 * @apiErrorExample {json} Error-Response:
 *     HTTP/1.1 200 OK
 *     {
 *        "status_code": 400,
 *        "message": "some reason",
 *        "data": ""
 *     }
 */
func (c *UserController) Login(ctx iris.Context) {
	c.Init(ctx)

	// 数据校验
	vErr := validates.LoginRequestValidate(ctx)
	if vErr != nil {
		c.output.Message(vErr.Error())
		return
	}

	// 登录
	authRepository := repositories.AuthRepository{}

	returnData, err := authRepository.Login(ctx)
	if err == nil {
		c.output.Success(returnData)
	} else {
		c.output.Message(err.Error())
	}
}

// 退出登录
func (c *UserController) Logout(ctx iris.Context) {
	c.Init(ctx)

	authRepository := repositories.AuthRepository{}

	returnData, err := authRepository.Logout(ctx)
	if err == nil {
		c.output.Success(returnData)
	} else {
		c.output.Message(err.Error())
	}
}
